using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Nb.FactoryAssistant.DAL.Abstracts;
using Nb.FactoryAssistant.DAL;
using Nb.FactoryAssistant.DAL.Abstracts.Entities;
using Nb.FactoryAssistant.Service.Abstracts;
using Nb.FactoryAssistant.Service.Abstracts.Dto;
using TianShu;
using TianShu.Domain;
using TianShu.Domain.Repositories;
using TianShu.EventBus;
using TianShu.Instructure.Abstracts;
using TianShu.Json;
using TianShu.Service.Dtos;
using TianShu.Service.Services.Crud;
using TianShu.Uow;
namespace Nb.FactoryAssistant.Service
{
    /// <summary>
    /// 仓库服务实现类
    /// </summary>
    public class WmsWarehouseServiceImpl : CrudServiceImpl<WmsWarehouse, Guid, WmsWarehouseListOutput, WmsWarehouseCreateUpdateInput>, IWmsWarehouseService
    {
        //private readonly IBattchNumberRuleDal _battchNumberRuleDal;
        private readonly IRepository<WmsWarehouseType, Guid> _warehouseTypeRepository;
        private readonly IRepository<WmsWarehouse, Guid> _warehouseRepository;
        /// <sumary>
        /// 仓库构造函数
        /// </sumary>
        public WmsWarehouseServiceImpl( IRepository<WmsWarehouseType, Guid> warehouseTypeRepository
                                        , IRepository<WmsWarehouse, Guid> warehouseRepository)
        {
            _warehouseTypeRepository = warehouseTypeRepository;
            _warehouseRepository = warehouseRepository;
        }

        public override async Task<PageResultDto<IEnumerable<WmsWarehouseListOutput>>> GetUrlParamAsync(List<UrlParams> urlParams)
        {
            Expression<Func<WmsWarehouse, bool>> whereCondition = e => true;
            if (urlParams.TryGetString("keyword", out string keyword))
            {
                whereCondition = e => e.Name.Contains(keyword) || e.Code.Contains(keyword);
            }
            var queryResult = await EntityRepository.GetListAsync(urlParams, whereCondition);

            var warehouseTypeDic = (await _warehouseTypeRepository.GetListAsync(e => true)).ToDictionary(e => e.Id, e => e);

            var data = ObjectMapper.Map<IEnumerable<WmsWarehouse>, IEnumerable<WmsWarehouseListOutput>>(queryResult.Item1);

            foreach (var item in data)
            {
                if (warehouseTypeDic.TryGetValue(item.WarehouseTypeId, out WmsWarehouseType outType))
                {
                    item.WarehouseTypeName = outType?.Name;
                }
            }

            return new PageResultDto<IEnumerable<WmsWarehouseListOutput>>(data, queryResult.Item2);
        }

        public override async Task<PageResultDto<WmsWarehouseListOutput>> InsertAsync(WmsWarehouseCreateUpdateInput dto)
        {
            var insertEntity = ObjectMapper.Map<WmsWarehouseCreateUpdateInput, WmsWarehouse>(dto);
            _warehouseRepository.TrySetId(insertEntity);

            insertEntity.QRCode = "";// await _battchNumberRuleDal.GetNextNumberAsync(BillOrderConsts.仓库二维码常量, insertEntity.Id);

            var codeEntity = await EntityRepository.FindAsync(e => e.Code == insertEntity.Code);
            if (codeEntity != null)
            {
                return new PageResultDto<WmsWarehouseListOutput>($"仓库code重复");
            }

            insertEntity = await EntityRepository.InsertAsync(insertEntity);
            var data = ObjectMapper.Map<WmsWarehouse, WmsWarehouseListOutput>(insertEntity);
            return new PageResultDto<WmsWarehouseListOutput>(data);
        }

        public override async Task<PageResultDto<WmsWarehouseListOutput>> UpdateAsync(Guid id, WmsWarehouseCreateUpdateInput dto)
        {
            var dbUpdateEntity = await EntityRepository.FindAsync(id);
            if (dbUpdateEntity == null)
            {
                return new PageResultDto<WmsWarehouseListOutput>($"The '{id}' is not found.");
            }

            if (dbUpdateEntity.Code != dto.Code)
            {
                var codeEntity = await EntityRepository.FindAsync(e => e.Code == dto.Code);
                if (codeEntity != null)
                {
                    return new PageResultDto<WmsWarehouseListOutput>($"仓库code重复");
                }
            }

            ObjectMapper.Map(dto, dbUpdateEntity);
            dbUpdateEntity = await EntityRepository.UpdateAsync(dbUpdateEntity);
            var data = ObjectMapper.Map<WmsWarehouse, WmsWarehouseListOutput>(dbUpdateEntity);

            return new PageResultDto<WmsWarehouseListOutput>(data);
        }


        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public override async Task<PageResultDto<bool>> DeleteAsync(IEnumerable<Guid> ids)
        {
            return await base.DeleteAsync(ids);
        }
    }
}
